<div class="row">
    <div class="col-sm-12">
        <?php $select_price = 0; ?>
<form id="barter-choose-form" action="/trades/barter/{{$barter_item['Trade']['id']}}" >
	{{if $datalist}}	
        
        <?php
            $tableHeaders =  $this->Html->tableHeaders(array(
                '',
                lang('name'),
                lang('price'),
                lang('end_date'),
                lang('opration'),
            ));
            $unselect_table = '<table class="table table-striped table-hover">'; //<thead>'.$tableHeaders.'</thead>

            $has_unselect_item = false;
            $un_select_rows = array();
            $select_item = '<ul>'; 
            foreach ($datalist AS $item) {

                if(is_array($_GET['barters']) && in_array($item['Trade']['id'],$_GET['barters']))
                {
                    $select_item .= '<li><input type="checkbox" class="barter_check" checked="checked" name="barters[]" value="'.$item['Trade']['id'].'"> '.$item['Trade']['name'].' $'.$item['Trade']['price'].' </li>';
                    $select_price += $item['Trade']['price'];
                }
                else{
                    $has_unselect_item = true;
                    $actions = ' ' . $this->Html->link(lang('edit'), array('controller'=>'Trades','action' => 'edit', $item['Trade']['id']));
                
                    $un_select_rows[] = array(
                        '<input type="checkbox" class="barter_check" name="barters[]" value="'.$item['Trade']['id'].'">',
                        $item['Trade']['name'],
                        '$'.$item['Trade']['price'],
                        $item['Trade']['end_date'],
                        $actions,
                    );
                }

                
            }
            $unselect_table .= $this->Html->tableCells($un_select_rows);
            $select_item .= '</ul>';
            $unselect_table .='</table>';
           
        ?>
        
        

    {{/if}}
    <?php $pay_price = ($barter_item['Trade']['price'] - $select_price); ?>
    <div class="well">
        <h2>{{$barter_item['Trade']['name']}} <span style="color:red;">${{$barter_item['Trade']['price']}}
        </span></h2>
        <p>{{usubstr(strip_tags($barter_item['Trade']['content']),0,300)}}</p>
    </div>
    <div class="panel panel-default">
      <div class="panel-heading">
        <h3 class="panel-title">Barter with</h3>
      </div>
      <div class="panel-body">
            <div  style="background-color: #fcf8f2;border-left: 3px solid #f0ad4e;padding:10px;margin-bottom:10px;">
                {{$select_item}}
               
                {{if $pay_price < 0 }}
                    <div class=" has-error">
                        {{lang('use_money')}}: $<input type="text" class="form-control" style="width:80px;display:inline;" name="price" value="{{$pay_price}}"> &nbsp;<input type="submit" class="btn btn-default disabled" value="barter this."/>
                    </div>
                    
                 <div class="alert alert-danger">
                    <strong>Warning!</strong> The total price of your selected item is bigger than the target price.
                    <p>Please cancel some slelect item.</p>

                 </div>
                {{else}}
                    <div class="form-group">
                        {{lang('use_money')}}: $<input type="text" class="form-control" style="width:80px;display:inline;" name="price" value="{{$pay_price}}"> &nbsp;<input type="submit" class="btn btn-success" value="barter this."/>
                    </div>         
                {{/if}}
               
            </div>
      </div>
    </div>
    
    <div class="panel panel-default">
      <div class="panel-heading">
        <h3 class="panel-title">Select Item To Barter</h3>
      </div>
      <div class="panel-body">
            {{if $has_unselect_item}}
                {{$unselect_table}}
                {{$page_navi}}
            {{/if}}
            <div>
                <a class="btn btn-info" target="_blank" href="{{$this->Html->url(array('action'=>'add'))}}">{{lang('submit_a_new_trade_to_barter_this')}}</a>
           </div>
      </div>
    </div>


</form>

    </div>
</div>


<script>
    $(function(){
        $(document).on('click','.barter_check',function(){
            $.ajax({
                type:'get',
                url:BASEURL+'/trades/choose/{{$barter_item['Trade']['id']}}',
                data: $('#barter-choose-form').serialize(),
                success:function(html){
                   $('#barter-choose-form').html( $('#barter-choose-form',html).html() );
                },
                dataType:'html'
                
            });            
        });
    })

</script>